package com.cdtu.labpreview.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.common.UserHolder;
import com.cdtu.labpreview.controller.vo.BaseIntroductionVo;
import com.cdtu.labpreview.entity.BaseIntroduction;
import com.cdtu.labpreview.entity.Teacher;
import com.cdtu.labpreview.entity.TeacherClass;
import com.cdtu.labpreview.mapper.BaseIntroductionMapper;
import com.cdtu.labpreview.mapper.ClassMapper;
import com.cdtu.labpreview.mapper.TeacherClassMapper;
import com.cdtu.labpreview.mapper.UserMapper;
import com.cdtu.labpreview.service.IBaseIntroductionService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 实验基本介绍模块(列出该实验的相关背景知识，以及应用情况)
 */
@RestController
@RequestMapping("/introduce")
@Api(tags = "实验基本介绍模块")
public class BaseIntroductionController {

    @Autowired
    private IBaseIntroductionService baseIntroductionService;

    @Autowired
    private TeacherClassMapper teacherClassMapper;

    @Autowired
    private BaseIntroductionMapper baseIntroductionMapper;

    /**
     * 老师获取自己已发布的实验基本介绍
     * @return
     */
    @GetMapping("/teacher")
    public Result getBaseIntroductionOfTeacher(){
        int tno = UserHolder.getUser().getUserNo();//需要拦截器拦截保存用户信息
        return baseIntroductionService.getBaseIntroductionOfTeacher(tno);
    }

    /**
     * 学生获取本班级对应的所有任科老师发布的实验基本介绍
     * @return
     */
    @GetMapping("/student")
    public Result getBaseIntroductionOfStudent(){
        int sno = UserHolder.getUser().getUserNo();
        return baseIntroductionService.getBaseIntroductionOfStudent(sno);
    }

    /**
     * 添加实验基本介绍（老师）
     * @return
     */
    @PostMapping
    public Result addBaseIntroduction(@RequestBody BaseIntroductionVo baseIntroductionVo){
        BaseIntroduction baseIntroduction=new BaseIntroduction();
        BeanUtils.copyProperties(baseIntroductionVo,baseIntroduction);
        return baseIntroductionService.addBaseIntroduction(baseIntroduction);
    }

    /**
     * 添加实验基本介绍到所有班级（老师）
     * @return
     */
    @PostMapping("/all")
    public Result addBaseIntroductionToAllClass(@RequestBody BaseIntroduction baseIntroduction){
        int userNo = UserHolder.getUser().getUserNo();
        baseIntroduction.setTno(userNo);
        List<TeacherClass> teacherClasses = teacherClassMapper.selectList(new LambdaQueryWrapper<TeacherClass>().eq(TeacherClass::getTno, userNo));
        List<Long> classIds = teacherClasses.stream().map(teacherClass -> teacherClass.getCid()).collect(Collectors.toList());
        for (int i = 0; i < classIds.size(); i++) {
            baseIntroduction.setClassId(classIds.get(i));
            baseIntroductionMapper.insert(baseIntroduction);
        }
        return Result.ok("添加成功！");
    }

    /**
     * 修改实验基本介绍（老师）
     * @return
     */
    @PutMapping
    public Result updateBaseIntroduction(@RequestBody BaseIntroduction baseIntroduction){
        return baseIntroductionService.updateBaseIntroduction(baseIntroduction);
    }

    /**
     * 删除实验基本介绍（老师）
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteBaseIntroduction(@PathVariable("id") long id){
        return baseIntroductionService.deleteBaseIntroduction(id);
    }

}
